//
//  RedPacketListCell.swift
//  MT
//
//  Created by 陈悦 on 2020/5/8.
//  Copyright © 2020 张强. All rights reserved.
//

import UIKit
import MTCategory

class RedPacketListCell: UITableViewCell {
    
    var isSend: Bool = false {
        didSet {
            if isSend {
                rightIcon.isHidden = true
                timeLab.textAlignment = .right
                moneyLabel.font = PingFangSCSemiboldFont(14)
                timeLab.snp.remakeConstraints { (make) in
                    make.right.equalTo(contentView).offset(-10)
                    make.centerY.equalTo(contentView)
                }
                
                moneyLabel.snp.remakeConstraints { (make) in
                    make.centerY.equalTo(detailLabel)
                    make.left.equalTo(detailLabel.snp.right).offset(4)
                }
            } else {
                stateLabel.isHidden = true
            }
        }
    }
    
    var model: MTRedPacketReportSubModel? {
        didSet {
            if model != nil {
                icon.image = UIImage(named: (model?.category == 2) ? "game_Red" : "Red_envelopes")
                detailLabel.text = model?.typeName
                timeLab.text = model?.createTime
                moneyLabel.text = "\(model?.money ?? 0)钻"
                stateLabel.text = stateStr(model: model!)
            }
        }
    }
    

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

    
    lazy var detailLabel: UILabel = {
        let l = UILabel()
        l.textColor = UIColor(hex:0x1d1a1a)
        l.font = PingFangSCMediumFont(12)
        l.text = "收到“扬州话痨联盟”俱乐部的福利红包"
        return l
    }()
    
    lazy var timeLab: UILabel = {
        let lab = UILabel()
        lab.textColor = UIColor(hex:0x9b9b9b)
        lab.font = PingFangSCMediumFont(10)
        lab.text = "2020-4-20  20:30"
        return lab
    }()
    
    lazy var icon: UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(named: "Red_envelopes")
        return imageView
    }()
    
    lazy var moneyLabel: UILabel = {
        let l = UILabel()
        l.textColor = UIColor(hex:0x1d1a1a)
        l.font = PingFangSCSemiboldFont(16)
        l.text = "+900"
        l.textAlignment = .right
        return l
    }()
    
    lazy var stateLabel: UILabel = {
        let l = UILabel()
        l.textColor = UIColor.text.x9b9b9b
        l.font = PingFangSCMediumFont(12)
        l.text = "已领完"
        return l
    }()
    
    lazy var rightIcon: UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(named: "dimond")
        return imageView
    }()
    
    lazy var lineView: UIView = {
        let v = UIView()
        v.backgroundColor = UIColor(hex:0xd8d8d8)
        return v
    }()
    
    
    class func reuseIdentity() -> String {
        return "RedPacketListCellID"
    }
    
    func bind(model: MTLuckyReportSubModel) {
            
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
        makeConstraint()
        contentView.backgroundColor = UIColor.theme.background
        backgroundColor = UIColor.theme.background
    }
    
    private func setupUI() {
        contentView.addSubview(detailLabel)
        contentView.addSubview(lineView)
        contentView.addSubview(moneyLabel)
        contentView.addSubview(icon)
        contentView.addSubview(rightIcon)
        contentView.addSubview(timeLab)
        contentView.addSubview(stateLabel)
    }
    
    private func makeConstraint() {
        
        icon.snp.makeConstraints { (make) in
            make.top.equalTo(contentView).offset(10)
            make.left.equalTo(contentView).offset(15)
            make.width.height.equalTo(44)
        }
        
        detailLabel.snp.makeConstraints { (make) in
            make.top.equalTo(contentView).offset(16)
            make.left.equalTo(icon.snp.right).offset(8)
        }
        
        timeLab.snp.makeConstraints { (make) in
            make.top.equalTo(detailLabel.snp.bottom).offset(4)
            make.left.equalTo(detailLabel)
            make.width.equalTo(300)
        }
        
        stateLabel.snp.makeConstraints { (make) in
            make.top.equalTo(detailLabel.snp.bottom).offset(4)
            make.left.equalTo(detailLabel)
            make.width.equalTo(300)
        }
        
        rightIcon.snp.makeConstraints { (make) in
            make.right.equalTo(contentView).offset(-10)
            make.centerY.equalTo(contentView)
            make.width.height.equalTo(24)
        }
        
        moneyLabel.snp.makeConstraints { (make) in
            make.right.equalTo(rightIcon.snp.left).offset(-2)
            make.centerY.equalTo(contentView)
        }
    

        lineView.snp.makeConstraints { (make) in
            make.left.bottom.right.equalToSuperview()
            make.height.equalTo(0.5)
        }
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }

}

extension RedPacketListCell {
    private func stateStr(model: MTRedPacketReportSubModel) -> String {
        var str = "已领完"
        switch model.state {
        case 0:
            str = "领取\(model.pullCount)/\(model.count)"
            break
        case 1:
            str = "已被领完"
            break
        case 2:
            str = "已过期"
            break
        default:
            str = ""
            break
        }
        return str
    }
}
